package com.company.dao;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.catalina.core.ApplicationContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.sql.Update;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao<T> extends HibernateDaoSupport {

	public T queryEntiy(final String hql, final Object... params) {
		return getHibernateTemplate().execute(new HibernateCallback<T>() {
			@Override
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				for (int i = 0; i < params.length; i++) {
					query.setParameter(i, params[i]);
				}
				List<T> list = query.list();
				if (list != null && list.size() > 0) {
					return list.get(0);
				}
				return null;
			}
		});
	}

	public T queryEntiy(final String hql) {
		return getHibernateTemplate().execute(new HibernateCallback<T>() {
			@Override
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				List<T> list = query.list();
				if (list != null && list.size() > 0) {
					return list.get(0);
				}
				return null;
			}
		});
	}

	public List queryEntiys(final String hql, final Object... params) {
		return (List) getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					@Override
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						for (int i = 0; i < params.length; i++) {
							query.setParameter(i, params[i]);
						}
						List tmp=query.list();
						Collections.reverse(tmp);
						return tmp;
					}
				});
	}

	public List queryEntiysById(final String hql, final int pageNum) {
		return (List) getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					@Override
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						query.setFirstResult(pageNum);
						query.setMaxResults(4);
						return query.list();
					}
				});
	}

	public List queryEntiysLast(final String hql, final int Num) {
		return (List) getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					@Override
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						query.setFirstResult(0);
						query.setMaxResults(Num);
						return query.list();
					}
				});
	}

	public void save(T t) {
		getHibernateTemplate().save(t);
	}

	public void delete(T t) {
		getHibernateTemplate().delete(t);
	}

	public void update(T t) {
		try {
			getHibernateTemplate().update(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
